package com.hwtx.form.domain.chart;

import com.hwtx.form.domain.chart.EChartsLineChartOptions.*;

import java.util.Arrays;

/**
 * 用于创建ECharts折线图配置的构建器类。
 * 提供流畅的API来构建复杂的图表配置。
 */
public class EChartsLineChartBuilder {
    
    private final EChartsLineChartOptions chartOptions;
    
    /**
     * 创建一个新的构建器实例
     */
    public EChartsLineChartBuilder() {
        chartOptions = new EChartsLineChartOptions();
    }
    
    /**
     * 创建一个带标题的新构建器实例
     * 
     * @param titleText 主标题文本
     * @return 构建器实例
     */
    public static EChartsLineChartBuilder createWithTitle(String titleText) {
        EChartsLineChartBuilder builder = new EChartsLineChartBuilder();
        EChartsLineChartOptions.Title title = new EChartsLineChartOptions.Title();
        title.setText(titleText);
        builder.chartOptions.setTitle(title);
        return builder;
    }
    
    /**
     * 设置图表标题
     * 
     * @param titleText 主标题文本
     * @return 构建器实例
     */
    public EChartsLineChartBuilder withTitle(String titleText) {
        if (chartOptions.getTitle() == null) {
            chartOptions.setTitle(new EChartsLineChartOptions.Title());
        }
        chartOptions.getTitle().setText(titleText);
        return this;
    }
    
    /**
     * 设置图表副标题
     * 
     * @param subtitleText 副标题文本
     * @return 构建器实例
     */
    public EChartsLineChartBuilder withSubtitle(String subtitleText) {
        if (chartOptions.getTitle() == null) {
            chartOptions.setTitle(new EChartsLineChartOptions.Title());
        }
        chartOptions.getTitle().setSubtext(subtitleText);
        return this;
    }
    
    /**
     * 设置标题位置
     * 
     * @param left 左侧位置 ('left', 'center', 'right')
     * @param top 顶部位置 ('top', 'middle', 'bottom')
     * @return 构建器实例
     */
    public EChartsLineChartBuilder withTitlePosition(String left, String top) {
        if (chartOptions.getTitle() == null) {
            chartOptions.setTitle(new EChartsLineChartOptions.Title());
        }
        chartOptions.getTitle().setLeft(left);
        chartOptions.getTitle().setTop(top);
        return this;
    }
    
    /**
     * 添加图例数据
     * 
     * @param legendNames 图例名称数组
     * @return 构建器实例
     */
    public EChartsLineChartBuilder withLegend(String... legendNames) {
        if (chartOptions.getLegend() == null) {
            chartOptions.setLegend(new EChartsLineChartOptions.Legend());
        }
        chartOptions.getLegend().setData(legendNames);
        return this;
    }
    
    /**
     * 设置图例位置
     * 
     * @param left 左侧位置 ('left', 'center', 'right')
     * @param top 顶部位置 ('top', 'middle', 'bottom')
     * @return 构建器实例
     */
    public EChartsLineChartBuilder withLegendPosition(String left, String top) {
        if (chartOptions.getLegend() == null) {
            chartOptions.setLegend(new EChartsLineChartOptions.Legend());
        }
        chartOptions.getLegend().setLeft(left);
        chartOptions.getLegend().setTop(top);
        return this;
    }
    
    /**
     * 设置图例方向
     * 
     * @param orient 方向 ('horizontal', 'vertical')
     * @return 构建器实例
     */
    public EChartsLineChartBuilder withLegendOrientation(String orient) {
        if (chartOptions.getLegend() == null) {
            chartOptions.setLegend(new EChartsLineChartOptions.Legend());
        }
        chartOptions.getLegend().setOrient(orient);
        return this;
    }
    
    /**
     * 设置提示框触发类型
     * 
     * @param trigger 触发类型 ('item', 'axis', 'none')
     * @return 构建器实例
     */
    public EChartsLineChartBuilder withTooltip(String trigger) {
        if (chartOptions.getTooltip() == null) {
            chartOptions.setTooltip(new EChartsLineChartOptions.Tooltip());
        }
        chartOptions.getTooltip().setTrigger(trigger);
        return this;
    }
    
    /**
     * 配置X轴
     * 
     * @param type 轴类型 ('value', 'category', 'time', 'log')
     * @param data 类目轴的数据
     * @return 构建器实例
     */
    public EChartsLineChartBuilder withXAxis(String type, Object... data) {
        if (chartOptions.getXAxis() == null) {
            chartOptions.setXAxis(new EChartsLineChartOptions.Axis());
        }
        chartOptions.getXAxis().setType(type);
        if (data != null && data.length > 0) {
            chartOptions.getXAxis().setData(data);
        }
        return this;
    }
    
    /**
     * 配置Y轴
     * 
     * @param type 轴类型 ('value', 'category', 'time', 'log')
     * @return 构建器实例
     */
    public EChartsLineChartBuilder withYAxis(String type) {
        if (chartOptions.getYAxis() == null) {
            chartOptions.setYAxis(new EChartsLineChartOptions.Axis());
        }
        chartOptions.getYAxis().setType(type);
        return this;
    }
    
    /**
     * 设置轴名称
     * 
     * @param xAxisName X轴名称
     * @param yAxisName Y轴名称
     * @return 构建器实例
     */
    public EChartsLineChartBuilder withAxisNames(String xAxisName, String yAxisName) {
        if (chartOptions.getXAxis() == null) {
            chartOptions.setXAxis(new EChartsLineChartOptions.Axis());
        }
        if (chartOptions.getYAxis() == null) {
            chartOptions.setYAxis(new EChartsLineChartOptions.Axis());
        }
        chartOptions.getXAxis().setName(xAxisName);
        chartOptions.getYAxis().setName(yAxisName);
        return this;
    }
    
    /**
     * 添加折线系列到图表
     * 
     * @param name 系列名称
     * @param data 数据点
     * @return 构建器实例
     */
    public EChartsLineChartBuilder addLineSeries(String name, Object... data) {
        EChartsLineChartOptions.Series series = new EChartsLineChartOptions.Series();
        series.setName(name);
        series.setType("line");
        series.setData(data);
        
        EChartsLineChartOptions.Series[] existingSeries = chartOptions.getSeries();
        if (existingSeries == null) {
            chartOptions.setSeries(new EChartsLineChartOptions.Series[]{series});
        } else {
            EChartsLineChartOptions.Series[] newSeries = Arrays.copyOf(existingSeries, existingSeries.length + 1);
            newSeries[existingSeries.length] = series;
            chartOptions.setSeries(newSeries);
        }
        return this;
    }
    
    /**
     * 添加面积系列到图表
     * 
     * @param name 系列名称
     * @param data 数据点
     * @return 构建器实例
     */
    public EChartsLineChartBuilder addAreaSeries(String name, Object... data) {
        EChartsLineChartOptions.Series series = new EChartsLineChartOptions.Series();
        series.setName(name);
        series.setType("line");
        series.setData(data);
        series.setAreaStyle(new EChartsLineChartOptions.AreaStyle());
        
        EChartsLineChartOptions.Series[] existingSeries = chartOptions.getSeries();
        if (existingSeries == null) {
            chartOptions.setSeries(new EChartsLineChartOptions.Series[]{series});
        } else {
            EChartsLineChartOptions.Series[] newSeries = Arrays.copyOf(existingSeries, existingSeries.length + 1);
            newSeries[existingSeries.length] = series;
            chartOptions.setSeries(newSeries);
        }
        return this;
    }
    
    /**
     * 设置图表网格布局
     * 
     * @param left 左边距
     * @param right 右边距
     * @param top 上边距
     * @param bottom 下边距
     * @param containLabel 网格是否包含标签
     * @return 构建器实例
     */
    public EChartsLineChartBuilder withGrid(Object left, Object right, Object top, Object bottom, Boolean containLabel) {
        if (chartOptions.getGrid() == null) {
            chartOptions.setGrid(new EChartsLineChartOptions.Grid());
        }
        chartOptions.getGrid().setLeft(left);
        chartOptions.getGrid().setRight(right);
        chartOptions.getGrid().setTop(top);
        chartOptions.getGrid().setBottom(bottom);
        chartOptions.getGrid().setContainLabel(containLabel);
        return this;
    }
    
    /**
     * 启用平滑线条
     * 
     * @return 构建器实例
     */
    public EChartsLineChartBuilder withSmoothLines() {
        if (chartOptions.getSeries() != null) {
            for (EChartsLineChartOptions.Series series : chartOptions.getSeries()) {
                if ("line".equals(series.getType())) {
                    series.setSmooth(true);
                }
            }
        }
        return this;
    }
    
    /**
     * 隐藏数据点标记
     * 
     * @return 构建器实例
     */
    public EChartsLineChartBuilder withoutSymbols() {
        if (chartOptions.getSeries() != null) {
            for (EChartsLineChartOptions.Series series : chartOptions.getSeries()) {
                if ("line".equals(series.getType())) {
                    series.setSymbol("none");
                }
            }
        }
        return this;
    }
    
    /**
     * 构建并返回图表配置
     * 
     * @return 配置好的图表选项
     */
    public EChartsLineChartOptions build() {
        return chartOptions;
    }
}